<?php
include "include/config.inc.php";

if(!isset($_SESSION['s_activId']))
{
  header('location:index.php');
}
$today = date("d-m-Y");
$msg="";
$m=0;
if(isset($_POST['loanBtn']))
{                                                 
  $partyNameGuj = (isset($_POST['partyNameGuj'])) ? $_POST['partyNameGuj']:"";
  $partyName    = (isset($_POST['partyName'])) ? $_POST['partyName'] : "";
  $address      = (isset($_POST['address'])) ? $_POST['address'] : "";
  $mobile       = (isset($_POST['mobile'])) ? $_POST['mobile'] : "";
  $otherInfo    = (isset($_POST['otherInfo'])) ? $_POST['otherInfo']:"";
  $value        = (isset($_POST['value'])) ? $_POST['value'] : "" ;
  $photo        = (isset($_FILES['photo']['name'])) ? $_FILES['photo']['name'] : "";
  
  if($_POST['partyNameSel'] == 0)
  { 
    $insPartyTb = "INSERT INTO `party` (`partyName`, `gPartyName`, `address`, `mobile`, `otherInfo`)
                        VALUES ('".$partyName."', 
                                '".$partyNameGuj."', 
                                '".addslashes($address)."',  
                                '".$mobile."',  
                                '".addslashes($otherInfo)."')";
    $insPartyTbRes = mysql_query($insPartyTb);
    $partyId = mysql_insert_id();
    
    if(isset($_FILES['photo']['name']))
    {
      $filename  =  pathinfo($_FILES['photo']['name']);
      $extension = $filename['extension'];
      $image     = "partyImg_".$partyId.".".$extension;
      $file      = "partyUplodeadImages/".$image;
      move_uploaded_file($_FILES['photo']['tmp_name'], $file); 
     
      $updateImage = "UPDATE party
                         SET photo = '".$image."'
                       WHERE partyId = ".$partyId;
      $updateImageRes = mysql_query($updateImage);
    }
  }
  else
  {
  	$img = $_FILES['photo']['name'];
    if( $img != "")
    {
    	$partyId   = $_POST['partyIdOld'];
    	$filename  =  pathinfo($_FILES['photo']['name']);
      $extension = $filename['extension'];
      $image     = "partyImg_".$partyId.".".$extension;
      $file      = "partyUplodeadImages/".$image;
      move_uploaded_file($_FILES['photo']['tmp_name'], $file);
        
      $updateParty = "UPDATE party 
                         SET partyName  = '".$partyName."',
                             gPartyName = '".$partyNameGuj."',
                             address    = '".addslashes($address)."',
                             mobile     = '".$mobile."',
                             photo      = '".$image."',
                             otherInfo  = '".addslashes($otherInfo)."'
                       WHERE partyId    = ".$partyId;
      $updatePartyRes = mysql_query($updateParty);
    }
    else
    {
      $updateParty = "UPDATE party 
                         SET partyName   = '".$partyName."',
                             gPartyName  = '".$partyNameGuj."',
                             address     = '".addslashes($address)."',
                             mobile      = '".$mobile."',
                             otherInfo   = '".$otherInfo."'
                       WHERE partyId     = ".$_POST['partyIdOld'];
      $updatePartyRes = mysql_query($updateParty);
    }
    if(!empty($_POST['partyNameSel']))
    {
      $partyId	= $_POST['partyNameSel'];	     	
    }
  }
  $loanVar = substr($partyName, 0, 1);
  
  ////set No Selection : Start
  $loanVarNo = "";
  $selectloanVarNo = "SELECT loanVar
                        FROM loan
                       WHERE loanVar = '".$loanVar."'";
  $selectloanVarNoResult = mysql_query($selectloanVarNo);
  $countRow = mysql_num_rows($selectloanVarNoResult);
  
  $countCharNo = $countRow + 1;
  
  $loanVarNo = $loanVar."".$countCharNo;
  
  $loanDate          = $_REQUEST['curDateYear']."-".$_REQUEST['curDateMonth']."-".$_REQUEST['curDateDay'];
  $lastInterastDate  = $_REQUEST['lastInstRecYear']."-".$_REQUEST['lastInstRecMonth']."-".$_REQUEST['lastInstRecDay'];
  $loadAmount        = $_POST['loadAmount'];	
  $item              = $_POST['item'];	
  $weight            = $_POST['weight'];	
  $itemLocation      = $_POST['itemLocation'];	
  $monthlyInterest   = $_POST['monthlyInterest'];	
  $monthlyInterestRs = $_POST['monthlyInterestRs'];
  
  if(isset($_FILES['file']['name']) && $_FILES['file']['name'] != "")
  {
  	$today = date('Y-m-d h:i:s');
    $filename  =  pathinfo($_FILES['file']['name']);
    $extension = $filename['extension'];
    $image = "loanImg_".strtotime($today).".".$extension;
    $file = "loanUplodeadImages/".$image;
    move_uploaded_file($_FILES['file']['tmp_name'], $file); 
  }
  $ckeckItemAvailablity = "SELECT itemId 
                             FROM item 
                            WHERE itemName ='".$item."' ";
  $ckeckItemAvailablityRes = mysql_query($ckeckItemAvailablity);
  $ckeckItemAvailablityRow = mysql_fetch_array($ckeckItemAvailablityRes);
  $ckeckItemAvailablityNumRow = mysql_num_rows($ckeckItemAvailablityRes);
  if($ckeckItemAvailablityNumRow > 0)
  {
    $itemId = $ckeckItemAvailablityRow['itemId'];
  }
  else
  {
    $insItem = "INSERT INTO `item` (`itemName`)
                     VALUES ('".$item."')";
    $insItemRes = mysql_query($insItem);
    $itemId = mysql_insert_id();
  }
  $ckeckItemlocationAvailablity = "SELECT itemlocationId
                                     FROM itemlocation 
                                    WHERE itemlocationName ='".addslashes($itemLocation)."' ";
  $ckeckItemlocationAvailablityRes = mysql_query($ckeckItemlocationAvailablity);
  $ckeckItemlocationAvailablityRow = mysql_fetch_array($ckeckItemlocationAvailablityRes);
  $ckeckItemlocationAvailablityNumRow = mysql_num_rows($ckeckItemlocationAvailablityRes);
  if($ckeckItemlocationAvailablityNumRow > 0)
  {
    $itemlocationId = $ckeckItemlocationAvailablityRow['itemlocationId'];
  }
  else
  {
    $insItemlocation = "INSERT INTO `itemlocation` (`itemlocationName`)
                             VALUES ('".addslashes($itemLocation)."')";
    $insItemlocationRes = mysql_query($insItemlocation);
    $itemlocationId = mysql_insert_id();
  }
  if($monthlyInterest == "")
  {
    $monthlyInterest = 0;
    $insLoan  = "INSERT INTO `loan` (`partyId`,`loanAmount`,`pendingAmount`, `itemId`, `weight`,`value`,`itemlocationId`,`itemImage`,`loanDate`, `lastInterestReceiveDate` ,`monthlyInterest`, `monthlyInterestRs`,`loanVar`,`loanVarNo`)
                      VALUES (".$partyId.", 
                              ".$loadAmount.", 
                              ".$loadAmount.", 
                              ".$itemId.",
                              ".$weight.",
                              ".$value.",
                              ".$itemlocationId.",
                              '".$image."',                        
                              '".$loanDate."',
                              '".$lastInterastDate."',
                              ".$monthlyInterest.", 
                              ".$monthlyInterestRs.",
                              '".$loanVar."',
                              '".$loanVarNo."')";
    $insLoanRes = mysql_query($insLoan);
    $lastId = mysql_insert_id();
    $insertRojmel = "INSERT INTO rojmel (date,amount,debitCredit,accountId,loanId)
                               VALUES ('".$loanDate."',".$loadAmount.",'Dr',4,".$lastId.")"; 
    $insertRojmelRes = mysql_query($insertRojmel);
    for($l=$loanDate;$l<=$lastInterastDate;$l=date ("Y-m-d", strtotime("+30 day", strtotime($l))))
      {
        $insertInterestRecived = "INSERT INTO `interestreceived` (`loanId`, `pendingAmount`, `interest`, `dateOfInstallment`, 
                                                                  `receivedDate`, `isReceive`)
                                       VALUES (".$lastId.",".$loadAmount.",".$monthlyInterestRs.",'".$l."','".$l."',1)";
        $insertInterestRecivedRes = mysql_query($insertInterestRecived);
        $insertRojmel = "INSERT INTO rojmel (date,amount,debitCredit,accountId,loanId)
                              VALUES ('".$l."',".$monthlyInterestRs.",'Cr',2,".$lastId.")";                                                                            
         $insertRojmelRes = mysql_query($insertRojmel);  
      }
    header("location:billListSimple.php?loanId=".$lastId);
  }
  else
  {  
    $insLoan  = "INSERT INTO  `loan` (`partyId`,`loanAmount`,`pendingAmount`, `itemId` ,`weight`,`value`,`itemlocationId`,`itemImage`,`loanDate`, `lastInterestReceiveDate` ,`monthlyInterest`, `monthlyInterestRs`,`loanVar`,`loanVarNo`)
                      VALUES (".$partyId.", 
                              ".$loadAmount.", 
                              ".$loadAmount.", 
                              ".$itemId.", 
                              ".$weight.",                              
                              ".$value.",
                              ".$itemlocationId.",
                              '".$image."',   
                              '".$loanDate."',
                              '".$lastInterastDate."',               
                              '".$monthlyInterest."', 
                              ".$monthlyInterestRs.",
                              '".$loanVar."',
                              '".$loanVarNo."')";
    $insLoanRes = mysql_query($insLoan);
    $lastId = mysql_insert_id();
    $insertRojmel = "INSERT INTO rojmel (date,amount,debitCredit,accountId,loanId)
                          VALUES ('".$loanDate."',".$loadAmount.",'Dr',4, ".$lastId.")";                                                                            
    $insertRojmelRes = mysql_query($insertRojmel);                                                                            
      for($l=$loanDate;$l<=$lastInterastDate;$l=date ("Y-m-d", strtotime("+30 day", strtotime($l))))
      {
        $insertInterestRecived = "INSERT INTO `interestreceived` (`loanId`, `pendingAmount`, `interest`, `dateOfInstallment`, 
                                                                  `receivedDate`, `isReceive`)
                                       VALUES (".$lastId.",".$loadAmount.",".$monthlyInterestRs.",'".$l."','".$l."',1)";
        $insertInterestRecivedRes = mysql_query($insertInterestRecived);
        $insertRojmel = "INSERT INTO rojmel (date,amount,debitCredit,accountId,loanId)
                              VALUES ('".$l."',".$monthlyInterestRs.",'Cr',2,".$lastId.")";                                                                            
         $insertRojmelRes = mysql_query($insertRojmel);  
      }
    header("location:billListSimple.php?loanId=".$lastId);
  }
}
$arrParty = array();
$i=0;
$selParty="SELECT `partyId`,`partyName` 
             FROM `party`";
$selPartyRes=mysql_query($selParty);
while($selPartyResRow=mysql_fetch_array($selPartyRes))
{
  $arrParty['partyName'][$i] = $selPartyResRow['partyName'];
  $arrParty['partyId'][$i]   = $selPartyResRow['partyId'];
  $i++;
}
//Item AutoComplete
include("bottom.php");
if(isset($_POST['partyNameSel']))
{
  $arrItem = array();
  $k = 0;
  $selItem="SELECT `itemName` 
              FROM `item`";
  $selItemRes=mysql_query($selItem);
  while($selItemResRow=mysql_fetch_array($selItemRes))
  {
    $arrItem['itemName'][$k] = $selItemResRow['itemName'];
    $k++;
  } 
  $smarty->assign('arrItem',$arrItem);
}
////fetch the party Name Over
$smarty->assign('arrParty',$arrParty);  
$smarty->assign("today",$today);
$smarty->assign('msg',$msg);
$smarty->display('loan.tpl');  
?>